using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Settings = global::Kdg.Tools.FilesCopier.Properties.Settings;

namespace Kdg.Tools.FilesCopier
{
    public partial class FormMain : Form
    {
        FilesCopierProcessor _processor = new FilesCopierProcessor();
        Size _oldSize;

        public FormMain()
        {
            InitializeComponent();

            _processor.Report += new ProgressChangedEventHandler(_processor_Report);
            _processor.Complete += new RunWorkerCompletedEventHandler(_processor_Complete);

            ofSource.SelectedPath = Settings.Default.sourcePath;
            ofDest.SelectedPath = Settings.Default.destPath;

            tbInclude.Text = Settings.Default.includeMask;
            tbExclude.Text = Settings.Default.excludeMask;
        }

        void _processor_Complete(object sender, RunWorkerCompletedEventArgs e)
        {
            ofDest.Enabled = ofSource.Enabled = true;
            tbExclude.Enabled = tbInclude.Enabled = true;

            btCancel.Enabled = !(btRun.Enabled = true);

            this.Size = _oldSize;
            pbCopyProgress.Value = 0;
            if (e.Result is FilesCopierProcessor.CopyCompleteEventArgs)
            {
                FilesCopierProcessor.CopyCompleteEventArgs arg = e.Result as FilesCopierProcessor.CopyCompleteEventArgs;

                lbCompleteReport.Text += string.Format("Copy complete:\r\n at {0:#.##} sec."
                    + "\r\nTotal files copied: {1}"
                    + "\r\nBytes copied {2}"
                    + "\r\nCanceled: {3}",
                    arg.WorkingTime.TotalSeconds,
                    arg.FilesCopied,
                    arg.BytesCopied,
                    arg.Canceled
                    );
            }
        }

        void _processor_Report(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is FilesCopierProcessor.CopyReportEventArgs)
            {
                FilesCopierProcessor.CopyReportEventArgs arg = e.UserState as FilesCopierProcessor.CopyReportEventArgs;
                lbFile.Text = arg.FileName;
                lbSource.Text = arg.SourceFolder;
                lbDest.Text = arg.DestinationFolder;

                pbCopyProgress.Value = arg.ProgressPercentage;
                lbSpeed.Text = string.Format("{0:#.##} byte/sec", arg.Speed);

                lbCompleteReport.Text+=arg.Message;
                if (arg.Exception != null)
                    lbCompleteReport.Text += string.Format("Catch exception '{0}'.\r\n", arg.Exception.Message);
            }
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Settings.Default.sourcePath = ofSource.SelectedPath;
            Settings.Default.destPath = ofDest.SelectedPath;

            Settings.Default.includeMask = tbInclude.Text;
            Settings.Default.excludeMask = tbExclude.Text;
        }

        private void btRun_Click(object sender, EventArgs e)
        {
            RunParams pars = new RunParams();
            pars.Source = ofSource.SelectedPath;
            pars.Destination = ofDest.SelectedPath;

            pars.IncludeMask = CreateMaskList(tbInclude.Text);
            pars.ExcludeMask = CreateMaskList(tbExclude.Text);

            _oldSize = this.Size;
            //this.Size = new Size(446, 334);

            ofDest.Enabled = ofSource.Enabled = false;
            tbExclude.Enabled = tbInclude.Enabled = false;

            btCancel.Enabled = !(btRun.Enabled = false);
            lbCompleteReport.Text = string.Empty;
            _processor.StartProcess(pars);
        }

        private List<string> CreateMaskList(string strMasks)
        {
            List<string> list = new List<string>();
            string[] masks = strMasks.Split(';');
            foreach (string mask in masks)
                list.Add(mask.Trim());

            return list;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            _processor.CancelAsync();
        }
    }
}